﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSGeo.MapGuide;
using System.Web.UI.HtmlControls;

namespace NegevGas.Extensions.ProjectManager
{
    public partial class GetSdfFile : System.Web.UI.Page
    {
        // Fields
        protected HtmlForm form1;

        // Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SessionId = base.Request["session"];
            this.MapName = base.Request["map"];
            this.LayoutId = base.Request["layout"];
            if (base.Request["layerId"] != null)
            {
                this.LayerDefinition = string.Empty;
                this.LayerId = Convert.ToInt32(base.Request["layerId"]);
                 
            }
            else
            {
                this.LayerId = 0;
                this.LayerDefinition = base.Request["layerDefinition"];
            }
            MgResourceIdentifier resId = null;
            //HtzMgHelper helper = new HtzMgHelper(this.SessionId, this.MapName, ;

            MgUserInformation userInfo = new MgUserInformation(this.SessionId);
            MgSiteConnection siteConnection = new MgSiteConnection();
            siteConnection.Open(userInfo);

            MgResourceService ResourceService = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
            MgFeatureService featureService = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);






            MgMap map = new MgMap();
            map.Open(ResourceService, this.MapName);
            //if (this.LayerId != 0)
            //{
                /*//ProjectCollection projectCollection = new ProjectsRepository().GetProjectCollection(this.LayerId);
                //if (!(!(projectCollection.LayoutId == this.LayoutId) || string.IsNullOrEmpty(projectCollection.FeatureId)))
                //{
            //        resId = new MgResourceIdentifier(projectCollection.FeatureId);
            //    //}
            //}
            //else*/ if (!string.IsNullOrEmpty(this.LayerDefinition))
            {
                MgLayer layer = new MgLayer(new MgResourceIdentifier(this.LayerDefinition), ResourceService);
                resId = new MgResourceIdentifier(layer.GetFeatureSourceId());
                
            }
            else
            {
                return;
            }
            if ((resId != null) && ResourceService.ResourceExists(resId))
            {
                string fileName = string.Format("{0}.sdf", resId.GetName());
                MgByteReader reader = ResourceService.GetResourceData(resId, fileName);
                byte[] buffer = new byte[0x800];
                base.Response.Clear();
                base.Response.ClearHeaders();

                base.Response.ContentType = reader.GetMimeType();
                base.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(fileName).Replace('+', '_')));
                int count = reader.Read(buffer, buffer.Length);
                while (count > 0)
                {
                    base.Response.OutputStream.Write(buffer, 0, count);
                    count = reader.Read(buffer, buffer.Length);
                    base.Response.Flush();
                }
                base.Response.Flush();
                base.Response.End();
                reader.Dispose();
            }
        }

        // Properties
        public string LayerDefinition { get; set; }

        public int LayerId { get; set; }

        public string LayoutId { get; set; }

        public string MapName { get; set; }

        public string SessionId { get; set; }
    }
}





